/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asistencia.DAO.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author rfarias
 */
public class Reloj {

    Calendar calendario = null;
    
    private String hora = "";
    private String minutos = "";
    private String segundos = "";
    private String dia = "";
    private String mes = "";
    private String año = "";
    private String strf = "";

    private int yea = 0;
    private int me = 0;
    private int di = 0;
    private int hor = 0;
    private int min = 0;
    private int seg = 0;


    public Reloj(){}

    private void getData()
    {
    calendario = Calendar.getInstance();
    try {
    // Create a URL for the desired page
    URL url = new URL("http://reloj.bibliometro.cl/horaj.php");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str="";
    strf = "";
    while ((str = in.readLine()) != null) {
        strf=strf+str;
    }
    in.close();
    }
    catch (MalformedURLException e) {}
    catch (IOException e) {}
    
    List<String> items = null;
    items = Arrays.asList(strf.split(","));

    yea = Integer.parseInt(items.get(0));
    me = Integer.parseInt(items.get(1));
    di = Integer.parseInt(items.get(2));
    hor = Integer.parseInt(items.get(3));
    min  = Integer.parseInt(items.get(4));
    seg = Integer.parseInt(items.get(5));

    //JOptionPane.showMessageDialog(null, seg);

    calendario.set(yea,me,di,hor,min,seg);
    }

    private String getHora()
    {
        hora = null;
        hora = calendario.get(Calendar.HOUR_OF_DAY)>9?""+calendario.get(Calendar.HOUR_OF_DAY):"0"+calendario.get(Calendar.HOUR_OF_DAY);
        //JOptionPane.showMessageDialog(null,hora);
        return hora;
    }

    private String getMinutos()
    {
        minutos = null;
        minutos = calendario.get(Calendar.MINUTE)>9?""+calendario.get(Calendar.MINUTE):"0"+calendario.get(Calendar.MINUTE);
        //JOptionPane.showMessageDialog(null,minutos);
        return minutos;
    }

    private String getSegundos()
    {
        segundos = calendario.get(Calendar.SECOND)>9?""+calendario.get(Calendar.SECOND):"0"+calendario.get(Calendar.SECOND);
        //JOptionPane.showMessageDialog(null,segundos);
        return segundos;
    }

    private String getDia()
    {
        dia = null;
        dia = calendario.get(Calendar.DAY_OF_MONTH)>9?""+calendario.get(Calendar.DAY_OF_MONTH):"0"+calendario.get(Calendar.DAY_OF_MONTH);
        //JOptionPane.showMessageDialog(null,dia);
        return dia;
    }

    private String getMes()
    {
        mes = null;
        mes = calendario.get(Calendar.MONTH)>9?""+calendario.get(Calendar.MONTH):"0"+calendario.get(Calendar.MONTH);
        //JOptionPane.showMessageDialog(null,mes);
        return mes;
    }

    //COnsulta
    private String getAño()
    {
        año = null;
        año = "20"+calendario.get(Calendar.YEAR);
        //JOptionPane.showMessageDialog(null,año);
        return año;
    }

    public String fechaDisp()
    {
        getData();
        String fecha = null;
        fecha = getDia()+"-"+getMes()+"-"+getAño();
        //JOptionPane.showMessageDialog(null,fecha);
        return fecha;
    }

        public String fechaDB()
    {
        getData();
        String fechaDB = null;
        fechaDB = getAño()+"-"+getMes()+"-"+getDia();
        //JOptionPane.showMessageDialog(null,fechaDB);
        return fechaDB;
    }

        public String horaFor()
    {
        getData();
        String horaFor = null;
        horaFor = getHora()+":" +getMinutos()+":"+getSegundos();
        //JOptionPane.showMessageDialog(null,horaFor);
        return horaFor;
    }

}
